package com.ssnst.wechat.service;

import com.ssnst.wechat.constant.WeChatConstant;
import com.ssnst.wechat.util.WeChatUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @ClassName：MwssageService
 * @Description：消息处理层
 * @Author：yz
 * @Time：2020/7/9 12:58
 * @Version：1.0
 */
@Service
public class MessageService {

    private static Logger log = Logger.getLogger(MessageService.class.getName());

    public String processRequest(HttpServletRequest request) {
        // xml格式的消息数据
        String respXml = null;
        // 默认返回的文本消息内容
        String respContent;
        try {
            // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = WeChatUtil.parseXml(request);
            for (Map.Entry<String, String> entry : requestMap.entrySet()) {
                System.out.println("key: "+entry.getKey()+", value: " +entry.getValue());
            }
            // 消息类型
            String msgType = requestMap.get(WeChatConstant.MsgType);
            String mes = null;
            // 文本消息
            if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_TEXT)) {
                mes = requestMap.get(WeChatConstant.Content);
                if (mes != null && mes.length() < 2) {
                    StringBuffer sb = new StringBuffer();
                    sb.append("<xml>");
                    sb.append("<ToUserName><![CDATA["+requestMap.get("FromUserName")+"]]></ToUserName>");
                    sb.append("<FromUserName><![CDATA["+requestMap.get("ToUserName")+"]]></FromUserName>");
                    sb.append("<CreateTime>"+requestMap.get("CreateTime")+"</CreateTime>");
                    sb.append("<MsgType><![CDATA[news]]></MsgType>");
                    sb.append("<ArticleCount>4</ArticleCount>");
                    sb.append("<Articles>");
                    for (int i = 0; i < 4; i++){
                        sb.append("<item>");
                        sb.append("<Title><![CDATA[博客园]]></Title>\");\n" +
                                "sb.append(\"<Description><![CDATA[我永远喜欢石原里美]]></Description>\");\n" +
                                "sb.append(\"<PicUrl><![CDATA[https://www.cnblogs.com/images/logo_small.gif]]></PicUrl>\");\n" +
                                "sb.append(\"<Url><![CDATA[https://home.cnblogs.com/u/yuan-zhou]]></Url>");
                        sb.append("</item>");
                    }
                    sb.append("</Articles>");
                    sb.append("</xml>");
                    respXml = sb.toString();
                    System.err.println(respXml);

                }
            }
            // 图片消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_IMAGE)) {
                respContent = "OK，这张图片我收到了，稍后我会看的，你退下吧！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 语音消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_VOICE)) {
                respContent = "你这说的啥玩意儿啊?我听不明白！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 视频消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_VIDEO)) {
                respContent = "不好意思，我正在上班中，不方便看视频！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 地理位置消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_LOCATION)) {
                respContent = "好的，你等着，我现在就过来找你！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 链接消息
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_LINK)) {
                respContent = "你……你又发了什么奇奇怪怪的网址？！";
                respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
            }
            // 事件推送
            else if (msgType.equals(WeChatConstant.REQ_MESSAGE_TYPE_EVENT)) {
                // 事件类型
                String eventType = requestMap.get(WeChatConstant.Event);
                // 关注
                if (eventType.equals(WeChatConstant.EVENT_TYPE_SUBSCRIBE)) {
                    respContent = "为什么现在才关注我，我都等傻了？";
                    respXml = WeChatUtil.sendTextMsg(requestMap, respContent);
                }
                // 取消关注
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_UNSUBSCRIBE)) {
                    log.info("用户"+requestMap.get("FromUserName")+"取消了关注");
                    // TODO 取消订阅后用户不会再收到公众账号发送的消息，因此不需要回复
                }
                // 扫描带参数二维码
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_SCAN)) {
                    // TODO 处理扫描带参数二维码事件
                }
                // 上报地理位置
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_LOCATION)) {
                    // TODO 处理上报地理位置事件
                }
                // 自定义菜单
                else if (eventType.equals(WeChatConstant.EVENT_TYPE_CLICK)) {
                    // TODO 处理菜单点击事件
                    log.info("自定义菜单生成中");
                    String eventKey = requestMap.get("EventKey");
                    if (eventKey.equals("11")) {
                        respContent = "天气预报菜单项被点击！";
                    } else if (eventKey.equals("12")) {
                        respContent = "公交查询菜单项被点击！";
                    } else if (eventKey.equals("13")) {
                        respContent = "周边搜索菜单项被点击！";
                    } else if (eventKey.equals("14")) {
                        respContent = "历史上的今天菜单项被点击！";
                    } else if (eventKey.equals("21")) {
                        respContent = "歌曲点播菜单项被点击！";
                    } else if (eventKey.equals("22")) {
                        respContent = "经典游戏菜单项被点击！";
                    } else if (eventKey.equals("23")) {
                        respContent = "美女电台菜单项被点击！";
                    } else if (eventKey.equals("24")) {
                        respContent = "人脸识别菜单项被点击！";
                    } else if (eventKey.equals("25")) {
                        respContent = "聊天唠嗑菜单项被点击！";
                    } else if (eventKey.equals("31")) {
                        respContent = "Q友圈菜单项被点击！";
                    } else if (eventKey.equals("32")) {
                        respContent = "电影排行榜菜单项被点击！";
                    } else if (eventKey.equals("33")) {
                        respContent = "幽默笑话菜单项被点击！";
                    }


            }
            }
            mes = mes == null ? "不知道你在干嘛" : mes;
            if (respXml == null) {
                respXml = WeChatUtil.sendTextMsg(requestMap, mes);
            }
            //System.out.println(respXml);
            return respXml;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";

    }
}
